<?php

namespace maotool\util;

use DOMDocument;
use maotool\view\html\Document;

class HtmlUtil
{

    public static function parser($content)
    {
        $document = new Document();
        $document->loadComponent($content);
        return $document;
    }

    public static function mini($content)
    {
        $content = str_replace("\r\n", '', $content); //清除换行符
        $content = str_replace("\n", '', $content); //清除换行符
        $content = str_replace("\t", '', $content); //清除制表符
        $pattern = array(
            "/> *([^ ]*) *</", //去掉注释标记
            "/\s+/",
            "/<!--[^!]*-->/",
            "/\" /",
            "/ \"/",
            "'/\*[^*]*\*/'"
        );
        $replace = array(
            ">\\1<",
            " ",
            "",
            "\"",
            "\"",
            ""
        );
        return preg_replace($pattern, $replace, $content);
    }

    public static function isDoctype($subject)
    {
        $pattern = '/^\s*<!DOCTYPE\s*html[^>]*>$/i';
        if (preg_match($pattern, $subject)) {
            return true;
        } else {
            return false;
        }
    }

}